import dash
from dash import html, Input, Output, dcc
import feffery_antd_components as fac
from urllib import parse

app = dash.Dash(__name__, update_title=None)
app.layout = html.Div(
    [
        # 注入url监听
        dcc.Location(id='url'),

        # 打印url各个主要的属性
        html.Div(id='url-params')
    ],
    style={
        'padding': '50px'
    }
)


@app.callback(
    Output('url-params', 'children'),
    [
        Input('url', 'href'),
        Input('url', 'pathname'),
        Input('url', 'search'),
        Input('url', 'hash')
    ]
)
def show_location_params(href, pathname, search, hash):
    if search:
        print({
            key: value[0]
            for key, value in parse.parse_qs(search.split('?')[1]).items()
        })
    return fac.AntdSpace(
        [
            fac.AntdText(f'href: {href}'),
            fac.AntdText(f'pathname: {pathname}'),
            fac.AntdText(f'search: {search}'),
            fac.AntdText(f'hash: {hash}'),
        ],
        direction='vertical'
    )


if __name__ == '__main__':
    app.run_server(debug=True)
